package Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs// ActionScript file
{
	import Classes.CustomEvents.CustomEvent;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GD_Zombie;
	import Classes.GameUtils.GU_dealBgSound;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.GV_ZombieHotPanel;
	import Classes.GameViews.ViewCartoonTargets.VCT_CommonZombie;
	import Classes.GameViews.ViewCartoonTargets.ViewCartoonTarget;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapTarget;
	import Classes.GameViews.ViewPanels.VP_BubbleUp;
	import Classes.GameViews.ViewPanels.VP_FastBuy_actPoint;
	import Classes.GameViews.ViewPanels.VP_FastBuy_weapon;
	import Classes.GameViews.ViewPanels.VP_errorPanel;
	import Classes.Interfaces.I_ControlTarget;
	import Classes.Managers.M_CommonZombieCartoons;
	
	import MV_BMP.MovieClip_BMP;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	/**
	 *僵尸类 
	 * @author 葛增文
	 * 
	 */	
	public class MN_Zombie extends MapNPC
	{
		private var _isChase:Boolean;/*是否处于追击状态*/
		private var _commonZombie:VCT_CommonZombie;/*僵尸动画*/
		private var _faceDir:int=1;/*面朝方向*/
		private var _actionName:String="";/*需要播放的动画名*/
		private var _hotPanel:GV_ZombieHotPanel;
		private var _bubble:Sprite=new Sprite();/*泡泡显示框*/
		/**
		 * 此僵尸是否导致追击背景音乐播放 
		 */		
		private var isPlayZombieMuisc:Boolean=false;/*是否在播放僵尸追击你的音乐*/
		
		public function MN_Zombie(gd:GD_Zombie):void
		{
			super(gd);
			this.init();
		}
		
		private function init():void
		{
			this.isWander=true;
			//this.nextWanderCount=Math.random()*300;
			//this.positionConditionsAry=["避难所外","没有NPC"];
			
			this.moveConditionsAry=["避难所外","没有NPC","没有角色","没有阻挡"];
			this.addListener();
			this.beginDrawMyView();
			this._hotPanel=new GV_ZombieHotPanel(this.zombieData);
			this._hotPanel.scaleX=0.5;
			this._hotPanel.scaleY=0.5;
			this.addChild(this.hotPanel);
			
//			this.hotPanel.y 	=	-this.commonZombie.height*this.commonZombie.scaleY + this.hotPanel.height;
			
			var head:DisplayObjectContainer		=	this.getPart("part0");
			var foot:DisplayObjectContainer		=	this.getPart("part12");
//			
			
//			var bg:Sprite		= this.hotPanel.interfaceUI["bg"]	as Sprite;
//			bubble.interfaceUI["y"]	=	bg.y + bg.height - 3;
			
//			this._bubble.y	=		-commonZombie.height/2+15;// + this._bubble.height;
			
//			this.hotPanel.y 	=	-commonZombie.height/2+15;
			this.hotPanel.y 	=	-head.y+50;
			this.hotPanel.visible=false;
		}
		
		override protected function beginDrawMyView():void
		{
			this.draw();
		}
		
		private function addListener():void
		{
			this.addEventListener(CustomEvent.CARTOONPLAYOVER,cartoonOverHandler);
			//this.addEventListener(MouseEvent.ROLL_OVER,overHandler);
			//this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
			//this.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(CustomEvent.CARTOONPLAYOVER,cartoonOverHandler);
			//this.removeEventListener(MouseEvent.ROLL_OVER,overHandler);
			//this.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
			//this.removeEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		private function overHandler(e:MouseEvent):void
		{
			this.hotPanel.visible=true;
		}
		
		private function outHandler(e:MouseEvent):void
		{
			this.hotPanel.visible=false;
		}
		
		/**
		 *动画播放完成的事件 
		 * @param e
		 * 
		 */		
		private function cartoonOverHandler(e:CustomEvent):void
		{
			switch(e.msgStr)
			{
				case "attack":
					break;
				case "beHitted":
					break;
				case "die":
					this.zombieData.release();
					//this.removeSign=1;
					if(this.isPlayZombieMuisc)
					{
						this.isPlayZombieMuisc=false;
						GU_dealBgSound.instance.zhuiJiZombieNum--;
						if(GU_dealBgSound.instance.zhuiJiZombieNum<=0)
						{
							if(!(GV_Map.instance.controlTarget as MMT_Character).mapGrid.isHaven && !(GV_Map.instance.controlTarget as MMT_Character).mapGrid.isWall )
							{
								GU_dealBgSound.instance.turnMusicByCharacter=1;
							}else{
								GU_dealBgSound.instance.turnMusicByCharacter=0;
							}
						}
					}
					break;
			}
			this.actionLocked=false;
			this.playNowStata();
		}
		
		/**
		 *创建一个僵尸 
		 * @param creatObj 创建数据对象
		 * @return 僵尸对象
		 * 
		 */		
		public static function creat(creatData:GD_Zombie):MN_Zombie
		{
			var creatTarget:MN_Zombie=new MN_Zombie(creatData);
			return creatTarget;
		}
		
		/**
		 *更新 
		 * 
		 */		
		override public function update():void
		{
			if(this._beginTimer || this._beginTimer2)return;			//如果正在播放出场动画
			
			if(this.removeSign)return;
			if(this.actionName!="")
			{
				this.commonZombie.playAction(String(this.transformDir(this.faceDir)),this.actionName);
				this.actionName="";
			}
			if(this.actionName=="die")return;
			super.update();
			if(!this.moveDir && !this.actionLocked)
			{
				if(this.isChase)
				{
					this.chaseCheck();
				}
				else
				{
					this.findCheck();
				}
			}
			
			if( (this.gameData as GD_Zombie).life<1)this.actDie();
			
		}
		
		/**
		 *追击检测 
		 * 
		 */		
		private function chaseCheck():void
		{
			if(null==this.mapGrid)return;
			var disToCharacter:Number=GV_Map.instance.getFindingPriceBetweenTwoPoints(this.mapGrid.coordinatePoint,GV_Map.instance.controlTarget.mapGrid.coordinatePoint);
			if(!GV_Map.instance.controlTarget.chaseEnabled || disToCharacter>this.chaseRange)
			{
				this.stopChase();
				return;
			}
			if(GV_Map.instance.controlTarget.attEnabled && disToCharacter<this.attRange)
			{
				this.actAtt();
				return;
			}
			var disX:Number=GV_Map.instance.controlTarget.mapGrid.atx-this.mapGrid.atx;
			var disY:Number=GV_Map.instance.controlTarget.mapGrid.aty-this.mapGrid.aty;
			var nextGrid1:GD_MapGrid;
			var nextGrid2:GD_MapGrid;
			if(disX)
			{
				if(disX>0)
				{
					nextGrid1=GV_Map.instance.getCoordinateGrid(new Point(this.mapGrid.atx+1,this.mapGrid.aty));
				}
				else
				{
					nextGrid1=GV_Map.instance.getCoordinateGrid(new Point(this.mapGrid.atx-1,this.mapGrid.aty));
				}
			}
			if(disY)
			{
				if(disY>0)
				{
					nextGrid2=GV_Map.instance.getCoordinateGrid(new Point(this.mapGrid.atx,this.mapGrid.aty+1));
				}
				else
				{
					nextGrid2=GV_Map.instance.getCoordinateGrid(new Point(this.mapGrid.atx,this.mapGrid.aty-1));
				}
			}
			var moveGrid1:GD_MapGrid;
			var moveGrid2:GD_MapGrid;
			if(Math.abs(disY)>Math.abs(disX))
			{
				moveGrid1=nextGrid2;
				moveGrid2=nextGrid1;
			}
			else
			{
				moveGrid1=nextGrid1;
				moveGrid2=nextGrid2;
			}
			if(moveGrid1 && this.moveCheck(moveGrid1))
			{
				this.beginMoveTo(moveGrid1);
			}
			else if(moveGrid2 && this.moveCheck(moveGrid2))
			{
				this.beginMoveTo(moveGrid2);
			}
			else
			{
				this.stopChase();
			}
		}
		
		/**
		 *停止追击 
		 * 
		 */		
		private function stopChase():void
		{
			this.isWander=true;
			this.isChase=false;
			this.playNowStata();
		}
		
		/**
		 *发觉检测 
		 * 
		 */		
		private function findCheck():void
		{
			if(null==this.mapGrid)return;
			var disToCharacter:Number=GV_Map.instance.getFindingPriceBetweenTwoPoints(this.mapGrid.coordinatePoint,GV_Map.instance.controlTarget.mapGrid.coordinatePoint);
			if(GV_Map.instance.controlTarget.chaseEnabled && disToCharacter<this.findRange)
			{
				this.isWander=false;
				this.isChase=true;
			}
		}
		
		/**
		 *僵尸攻击 
		 * 
		 */		
		private function actAtt():void
		{
			this.actionLocked=true;
			var attTarget:I_ControlTarget=GV_Map.instance.controlTarget;
			attTarget.actBeHitted(this);
			this.actionName="attack";
		}
		
		/**
		 *僵尸被击 
		 * 
		 */		
		public function actBeHitted():void
		{
			this.actionLocked=true;
			this.actionName="beHitted";
		}
		
		/**
		 *僵尸死亡 
		 * 
		 */		
		public function actDie():void
		{
			this.actionLocked=true;
			this.actionName="die";
		}
		
		/**
		 *目标格移动检测 
		 * @param grid
		 * @return 
		 * 
		 */		
		private function moveCheck(grid:GD_MapGrid):Boolean
		{
			return grid.conditionsCheck(this.moveConditionsAry);
		}
		
		/**
		 *绘制 
		 * 
		 */		
		private function draw():void
		{
			if(!this.zombieData)return;
			if(this.commonZombie)
			{
				this.removeChild(this.commonZombie);
			}
			this._commonZombie=new VCT_CommonZombie(this.zombieData.zombieSkinType);
			//this._cvi_zombie=new CVI_Zombie(this.zombieData.cartoonSourceName);
			this.addChild(this.commonZombie);
			this.commonZombie.playAction("leftUp","stand");
			this.commonZombie.x=10;
			this.commonZombie.y=5;
			
			
			this.addChild(this._bubble);
			
			this.beginCartoon();
		}
		
		
		/**
		 *播放僵尸当前状态（移动或停止移动） 
		 * 
		 */		
		private function playNowStata():void
		{
			if(this.moveDir)
			{
				this.faceDir=this.moveDir;
				this.actionName="move";
				
				if( Math.random()*100 < 3)this.addBubble();		//10%概率冒泡
			}
			else
			{
				this.actionName="stand";
			}
		}
		
		/**
		 *覆写对象被点击方法 
		 * @param e
		 * 
		 */		
		override public function beClicked(e:MouseEvent):void
		{
			if(VP_FastBuy_actPoint.instance.check()>=3)return;
			if(VP_FastBuy_weapon.instance.check()>=3)return;	
			//如果血量为0 则不能选取
			if(this.zombieData.life<1)return;
			GV_Map.instance.controlTarget.setActTarget(this);
			
			//			this.setStates("10002006^10002004", true);
		}
		
		/**
		 *点击触发 
		 * 
		 */		
		/*private function clickHandler(e:MouseEvent):void
		{
		if(GV_Map.instance.preventClick)
		{
		return;
		}
		GV_Map.instance.controlTarget.setActTarget(this);
		}*/
		
		/**
		 *下一步行为 
		 * 
		 */		
		override protected function nextStep():void
		{
			super.nextStep();
			if(this.isChase)
			{
				this.chaseCheck();
			}
		}
		
		/**
		 *释放 
		 * 
		 */		
		override public function release():void
		{
			this.removeListener();
			super.release();
			
			if(null != this.commonZombie)
			this.commonZombie.release();
			this._commonZombie=null;
			
			if(null != this.commonZombie)
			this.hotPanel.release();
			this._hotPanel=null;
			
			if(this._beginTimer){
				this._beginTimer.removeEventListener(TimerEvent.TIMER,this.beginCartoon);
				this._beginTimer=null;
			}
			if(this._beginTimer2)
			{
				this._beginTimer2.removeEventListener(TimerEvent.TIMER,this.endBeginCartoonHandler);
				endBeginCartoonHandler(null);
			}
		}
		
		/*getter & setter*/
		/**
		 * 是否处于追击状态 
		 * @return 
		 */		
		public function get isChase():Boolean
		{
			return _isChase;
		}
		
		public function set isChase(value:Boolean):void
		{
			_isChase = value;
			if(this.isChase)
			{
				this.setState(10002003,true);
				switch(this.zombieData.zombieKind)
				{
					case "221":
					case "222":
					case "223":
						GU_dealBgSound.instance.zhuiJiZombieNum++;
						//						GU_dealBgSound.instance.turnMusicByCharacter=2;//以后要改的
						this.isPlayZombieMuisc=true;
						//						trace(this.zombieData.zombieType+"追击了");
						break;
				}
				if( Math.random()*100 < 3)this.addBubble();		//3%概率冒泡
			}
			else
			{	
				if(this.isPlayZombieMuisc)
				{
					//					trace(this.zombieData.zombieType+"眉追击了");
					GU_dealBgSound.instance.zhuiJiZombieNum--;
					this.isPlayZombieMuisc=false;
					if(GU_dealBgSound.instance.zhuiJiZombieNum<=0)
					{
						if(!(GV_Map.instance.controlTarget as MMT_Character).mapGrid.isHaven && !(GV_Map.instance.controlTarget as MMT_Character).mapGrid.isWall )
						{
							GU_dealBgSound.instance.turnMusicByCharacter=1;
						}else{
							GU_dealBgSound.instance.turnMusicByCharacter=0;
						}
					}
				}
				
				this.setState(10002003,false);
			}
		}
		
		public function get zombieData():GD_Zombie
		{
			return this.gameData as GD_Zombie;
		}
		
		public function get attRange():int
		{
			return this.zombieData.attRange;
		}
		
		public function get findRange():int
		{
			return this.zombieData.findRange;
		}
		
		public function get chaseRange():int
		{
			return this.zombieData.chaseRange;
		}
		
		override public function get speed():int
		{
			if(this.isChase)return this.zombieData.speed+20;
			return this.zombieData.speed;
		}
		
		public function get commonZombie():VCT_CommonZombie
		{
			return _commonZombie;
		}
		
		override public function set moveDir(value:int):void
		{
			super.moveDir=value;
			this.playNowStata();
		}
		
		public function get faceDir():int
		{
			return _faceDir;
		}
		
		public function set faceDir(value:int):void
		{
			_faceDir = value;
		}
		
		override public function get actionEnabled():Boolean
		{
			return !this.zombieData.isDead
		}
		
		public function get actionName():String
		{
			return _actionName;
		}
		
		public function set actionName(value:String):void
		{
			_actionName = value;
		}
		
		public function get hotPanel():GV_ZombieHotPanel
		{
			return _hotPanel;
		}
		
		override public function get correctPositionConditionsAry():Array
		{
			if((this.gameData as GD_Zombie).zombieType.toString().slice(0,3)=="211"  || this.mapGrid.isHaven)return ["避难所内","没有建筑","没有围墙"];
			return ["避难所外","没有建筑"];
		}
		
		override public function get resetPositionConditionsAry():Array
		{
			if((this.gameData as GD_Zombie).zombieType.toString().slice(0,3)=="211" || this.mapGrid.isHaven)return ["避难所内","没有建筑","没有NPC","没有围墙"];
			return ["避难所外","没有NPC","没有建筑"];
		}
		
		override public function get selectPriority():int
		{
			return 30;
		}
		
		
		/**
		 *覆写碰撞检测 
		 * @param checkX
		 * @param checkY
		 * @return 
		 * 
		 */		
		override public function hitTestPointCheck(checkX:Number, checkY:Number):Boolean
		{
			if(this.commonZombie && super.hitTestPointCheck(checkX,checkY))
			{					
				return this.commonZombie.hitTest(new Point(checkX,checkY));
				/*var checkTarget:MovieClip_BMP=commonZombie.getChildAt(0) as MovieClip_BMP;
				if(checkTarget==null)return false;
				return checkTarget.hitTest(new Point(checkX,checkY));*/
			}
			return false;
		}
		
		/**
		 * 覆定发光，加上显示热感。去掉了，mouser_out,mouser_ROLL_OVER事件 
		 * @param value
		 * 
		 */
		override public function setGlowFilter(value:Boolean):void
		{
			if(this.hotPanel){
				this.hotPanel.visible	= 	value;
			}
			super.setGlowFilter(value);
		}
		
		/**
		 *加一个泡泡 
		 * @param bubble
		 */
		public function addBubble(txt:String=""):void
		{
			cleanBubble();
			if(this.gameData==null)return;
			if(txt=="")txt = (this.gameData as GD_Zombie).currentZombieObj.zt_zombie_desc;
			
			var bubble:VP_BubbleUp	=	new VP_BubbleUp();
			
			//			var rd:Number	=	Math.random()*5;
			//			var t:String	=	"";//txt;
			//			for(var i:uint=0; i<rd; i++)
			//			{
			//				t += txt;
			//			}
			
			bubble.setPopTxts(txt);
			
			
			this._bubble.addChild(bubble);
			
			//			this._bubble.y	=this._commonZombie.y-this._commonZombie.height+70;
			this._bubble.scaleX		=	0.8;
//			this._bubble.scaleY		=   1;
			
			var txtf:TextField	= bubble.interfaceUI["txtF"] as TextField;
			var bg:Sprite		= bubble.interfaceUI["bg"]	as Sprite;
			if(txtf.numLines <= 2)bg.height	= txtf.height	=	bg.height/3 * txtf.numLines;
			bg.height	=	txtf.height + 10;
			bubble.interfaceUI["jt"]["y"]	=	bg.y + bg.height - 3;
			
//			this._bubble.y = this.height - commonZombie.height - bg.height;//this._bubble.height + 10;
						this._bubble.y	=		-commonZombie.height/2+15;// + this._bubble.height;
			
			var tm:Timer	=	new Timer(5000,1);
			tm.addEventListener(TimerEvent.TIMER,function(e:Event):void{
				cleanBubble();
			});
			tm.start();
		}
		/**
		 *清除泡泡 
		 */
		public function cleanBubble():void
		{
			while(this._bubble.numChildren>0)this._bubble.removeChildAt(0)
		}
		
		public function getPart(partName:String):DisplayObjectContainer
		{
			return M_CommonZombieCartoons.instance.getPart(this.zombieData.zombieSkinType, this._commonZombie.nowPlayingName, partName);
		}
		
		
		private var _beginTimer:Timer=null;
		private var _beginTimer2:Timer=null;
		private var _isBC:Boolean=false;
		
		//*************************************************** 播放开始出现动画 ************* 
		/**
		 *播放开始出现动画 
		 * 
		 */
		public function beginCartoon():void
		{
//			if(_isBC)return;
//			_isBC = true;
//			trace( GV_Map.instance.localToGlobal( new Point(this.x, this.y)));
//			if( !GV_Map.instance.checkInView(this)){trace("noInView");return;}
//			else{trace("inView");}
			
			this.commonZombie.visible	=	false;
			stopState();
			
			_beginTimer		=	new Timer(6000,1);
			_beginTimer.addEventListener(TimerEvent.TIMER,beginCartoon2);
			_beginTimer.start();
		}
		
		public function beginCartoon2(e:Event):void
		{
			if(_beginTimer==null)return;
			_beginTimer.removeEventListener(TimerEvent.TIMER,beginCartoon2);
			_beginTimer	=	null;
			
			_beginTimer2		=	new Timer(4000,1);
			_beginTimer2.addEventListener(TimerEvent.TIMER,endBeginCartoonHandler);
			_beginTimer2.start();
			
			this.setState(10002008,true);
			
		}
		
		public function endBeginCartoonHandler(e:Event):void
		{
			if(_beginTimer2==null)return;
			_beginTimer2.removeEventListener(TimerEvent.TIMER,endBeginCartoonHandler);
			_beginTimer2	=	null;
			
			if(this.commonZombie){
				
				this.setState(10002008,false);
				this.commonZombie.visible	=	true;
				playState();
			}
		}
		
		public function stopState():void
		{
			if( (this.gameData as GD_Zombie).currentZombieObj.zt_zombie_power!="no")
			setStates((this.gameData as GD_Zombie).currentZombieObj.zt_zombie_power,false);
//			view.setState(10004008,false)
		}
		
		public function playState():void
		{
			if( (this.gameData as GD_Zombie).currentZombieObj.zt_zombie_power!="no")
			setStates((this.gameData as GD_Zombie).currentZombieObj.zt_zombie_power,true);
		}
		
	}
}